package com.woniuxy.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.authentication.PermissionAuth;
import com.woniuxy.entitys.*;
import com.woniuxy.log.LogAnnotation;
import com.woniuxy.remote.RemoteHospitalService;
import com.woniuxy.services.FaultService;
import com.woniuxy.utils.ResultVO;
import com.woniuxy.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 维修列表
 */
@RestController
@RequestMapping("/fault")
public class FaultController {

    @Autowired
    private FaultService faultService;
    @Autowired
    private UserUtils userUtils;
    @Autowired
    private RemoteHospitalService remoteHospitalService;

    /**
     * 查询所有故障记录(分页)
     * faultStatusValue参数根据状态查询：(null、0、1、2、3)全部、待维修、修复中、已修复、无法修复
     * @param map
     * @return
     */
    @PostMapping("/getAllFault")
    public ResultVO getAllFault(@RequestBody Map<String, Integer> map){
        List<TbMalfunction> allFault = faultService.getAllFault(map.get("pageNum"), map.get("pageSize"), map.get("faultStatusValue"));
        if (allFault.size() == 0) {
            return ResultVO.failure("没有数据");
        }
        PageInfo<TbMalfunction> pageInfo = new PageInfo(allFault);
        //医院对应科室树结构
        List hospitals = (List) remoteHospitalService.getHospitalAndDepartment().getData();
        Map<String, Object> response = new HashMap<>();
        response.put("pageInfo", pageInfo);
        response.put("hospitals", hospitals);
        return ResultVO.success("查询成功", response);
    }

    /**
     * 根据id查询故障记录
     * @return
     */
    @GetMapping("/getAllFaultById/{maid}")
    @PermissionAuth(value = "dmlFault")
    public ResultVO getAllFaultById(@PathVariable Integer maid){
        TbMalfunction tbMalfunction = faultService.getAllFaultById(maid);
        if (tbMalfunction == null) {
            return ResultVO.failure("没有数据");
        }
        return ResultVO.success("查询成功", tbMalfunction);
    }

    /**
     * 增加故障记录
     *
     * @return
     */
    @PostMapping("/addFault")
    @PermissionAuth(value = "dmlFault")
    @LogAnnotation(info = "增加故障记录")
    public ResultVO addFault(@RequestBody TbMalfunction tbMalfunction) {
        if (faultService.addFault(tbMalfunction)) {
            return ResultVO.success("添加成功");
        }
        return ResultVO.failure("添加失败");
    }

    /**
     * 修改故障记录
     *
     * @return
     */
    @PostMapping("/updateFault")
    @PermissionAuth(value = "dmlFault")
    @LogAnnotation(info = "修改故障记录")
    public ResultVO updateFault(@RequestBody TbMalfunction tbMalfunction) {
        if (faultService.updateFault(tbMalfunction)) {
            return ResultVO.success("修改成功");
        }
        return ResultVO.failure("修改失败");
    }

    /**
     * 点击新增模态框后
     *
     * @return
     */
    @GetMapping("/showAdd")
    @PermissionAuth(value = "dmlFault")
    public ResultVO showAdd() {
        TbUser user = userUtils.getLoginUser().getUser();
        //科室负责人对象
        TbSection sectionInfo = faultService.getSectionInfo(user.getUid());
        //自己科室状态为非异常的床位
        List<TbBed> noFaultBed = faultService.getNoFaultBed(sectionInfo.getDid());
        Map<String, Object> map = new HashMap<>();
        map.put("sectionInfo", sectionInfo);
        map.put("noFaultBed", noFaultBed);
        return ResultVO.success("查询成功", map);
    }

    /**
     * 模糊查询维修记录(分页)
     *
     * @return
     */
    @PostMapping("/getAllFaultByFuzz")
    public ResultVO getAllFaultByFuzz(@RequestBody Map<String, String> map) {
        List<TbMalfunction> allFault = faultService.getAllFaultByFuzz(Integer.parseInt(map.get("pageNum")), Integer.parseInt(map.get("pageSize")), map.get("searchValue"));
        if (allFault.size() == 0) {
            return ResultVO.failure("没有数据");
        }
        PageInfo<TbMalfunction> pageInfo = new PageInfo(allFault);
        return ResultVO.success("查询成功", pageInfo);
    }

    /**
     * 增加维修记录时，模态框中查询所有故障信息
     */
    @GetMapping("/list")
    public ResultVO getFaultList() {
        List<TbMalfunction> malfunctionList = faultService.getFaultList();
        return malfunctionList != null ? ResultVO.success("success", malfunctionList) : ResultVO.failure("failed");
    }
    /*
     * 级联查询维修记录(分页)
     * 级联：所属医院 => 所属科室
     * @return
     */
    @PostMapping("/getAllFaultByCascade")
    @PermissionAuth(value = "cascadeFault")
    public ResultVO getAllFaultByCascade(@RequestBody Map<String, Object> map) {
        List<TbMalfunction> allFaultByCascade = faultService.getAllFaultByCascade((Integer) map.get("pageNum"), (Integer) map.get("pageSize"), (List) map.get("cascadeValue"));
        if (allFaultByCascade.size() == 0) {
            return ResultVO.failure("没有数据");
        }
        PageInfo<TbMalfunction> pageInfo = new PageInfo(allFaultByCascade);
        return ResultVO.success("查询成功", pageInfo);
    }

    /**
     * 维修人员批量受理维修记录
     *
     * @return
     */
    @PostMapping("/acceptFailure")
    @PermissionAuth("acceptFailure")
    public ResultVO acceptFailure(@RequestBody Map<String, Object> map) {
        if (faultService.acceptFailure((List) map.get("maids"))) {
            return ResultVO.success("受理成功");
        } else {
            return ResultVO.failure("受理失败");
        }
    }

    /**
     * 科室负责人批量撤销维修记录
     *
     * @return
     */
    @PostMapping("/cancelFailure")
    @PermissionAuth("cancelFailure")
    public ResultVO cancelFailure(@RequestBody Map<String, Object> map) {
        if (faultService.cancelFailure((List) map.get("maids"), (List) map.get("bids"))) {
            return ResultVO.success("撤销成功");
        } else {
            return ResultVO.failure("撤销失败");
        }
    }
}
